using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(CarEntityController))]
public class CarEntityControllerEditor : UnityEditor.Editor
{
    public string lightKeyword = "_ENABLEEMISSIVE_ON";
    public string lightMatPrefix = "_lamplight";
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        if (GUILayout.Button("一键绑定车灯"))
        {
            SearchLightMeshRenderer();
        }
        /*if (GUILayout.Button("一键开车灯"))
        {
            EnableLight(true);
        }
        if (GUILayout.Button("一键关车灯"))
        {
            EnableLight(false);
        }*/
    }
    
    public void SearchLightMeshRenderer()
    {
        CarEntityController cec = target as CarEntityController;
        if (cec)
        {
            MeshRenderer[] mrs = cec.gameObject.GetComponentsInChildren<MeshRenderer>();
            foreach (MeshRenderer mr in mrs)
            {
                if (mr.sharedMaterials!=null)
                {
                    foreach (Material mat in mr.sharedMaterials)
                    {
                        if (mat.shader.name == cec.lightShaderName && mat.name.Contains(lightMatPrefix))
                        {
                            cec.SetLightMeshRenderer(mr);
                            EditorUtility.DisplayDialog("设置车灯MeshRender","设置成功!","确定");
                            EditorUtility.SetDirty(target);
                            AssetDatabase.SaveAssets();
                            AssetDatabase.Refresh();
                            return;
                        }
                    }
                }
            }
            EditorUtility.DisplayDialog("设置车灯MeshRender","设置失败,请检查是否有带有 " +cec.lightShaderName + " shader的车灯MeshRender" ,"确定");
        }
    }

    public void EnableLight(bool enable)
    {
        CarEntityController cec = target as CarEntityController;
        if (cec)
        {
            MeshRenderer[] mrs = cec.gameObject.GetComponentsInChildren<MeshRenderer>();
            foreach (MeshRenderer mr in mrs)
            {
                if (mr.materials!=null)
                {
                    foreach (Material mat in mr.sharedMaterials)
                    {
                        if (mat.shader.name == cec.lightShaderName)
                        {
                            if (enable)
                            {
                                mat.EnableKeyword(lightKeyword);
                            }
                            else
                            {
                                mat.DisableKeyword(lightKeyword);
                            }
                            
                            EditorUtility.DisplayDialog(enable?"开车灯MeshRender":"关车灯MeshRender","成功!","确定");
                            EditorUtility.SetDirty(mat);
                            AssetDatabase.SaveAssets();
                            AssetDatabase.Refresh();
                            return;
                        }
                    }
                }
            }
        }
    }
}
